package com.ktjy.temp.web.controller;

import com.ktjy.temp.utils.ApiService;
import com.ktjy.temp.web.service.TempService;
import jakarta.annotation.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class IndexController {


    @Resource
    ApiService apiService;

    @Resource
    TempService tempService;

    /**
     * 获得硬件设备列表
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/getDeviceDetailsList")
    public Object getDeviceDetailsList() {
        //List<DeviceDetails> list = deviceDetailsService.getList();
        ResponseEntity<String> stringResponseEntity =
                apiService.sendGetRequest("http://localhost:9999/getDeviceDetailsList", String.class);
        if (stringResponseEntity.getStatusCode().is2xxSuccessful()) {
            return stringResponseEntity.getBody();
        } else {
            return "API调用失败: " + stringResponseEntity.getStatusCode();
        }
    }

    /**
     * 根据设备id获取该设备的温湿度数据
     *
     * @param sensorId
     * @param size
     * @return
     */
    @ResponseBody
    @GetMapping("/getDeviceDataBySensorIdList")
    public Object getDeviceDataBySensorIdList(String sensorId, int size) {
        ResponseEntity<String> stringResponseEntity =
                apiService.sendGetRequest("http://localhost:9999/getDeviceDataBySensorIdList?sensorId=" + sensorId + "&size=" + size, String.class);
        if (stringResponseEntity.getStatusCode().is2xxSuccessful()) {
            return stringResponseEntity.getBody();
        } else {
            return "API调用失败: " + stringResponseEntity.getStatusCode();
        }
    }

    @GetMapping("/main")
    public String main() {
        return "main";
    }


    @ResponseBody
    @GetMapping("/getDeviceDetailsListFeign")
    public Object getDeviceDetailsListFeign() {
//        ResponseEntity<String> stringResponseEntity =
//                apiService.sendGetRequest("http://localhost:9999/getDeviceDetailsList", String.class);
//        if (stringResponseEntity.getStatusCode().is2xxSuccessful()) {
//            return stringResponseEntity.getBody();
//        } else {
//            return "API调用失败: " + stringResponseEntity.getStatusCode();
//        }
        Object deviceDetailsList = tempService.getDeviceDetailsList();
        return deviceDetailsList;
    }

}
